<?php

class Public_Post_EditAction extends BlogPublicBaseAction
{
    /**
     * HTTP GET request handler
     *
     * @param AgaviRequestDataHolder $rd
     * @return mixed Target view
     */
    public function executeRead(AgaviRequestDataHolder $rd)
    {
        return 'Input';
    }
    
    /**
     * Process HTTP POST request
     *
     * @param AgaviRequestDataHolder $rd
     * @return mixed Target view
	 */
    public function executeWrite(AgaviRequestDataHolder $rd)
    {
        $params = $rd->getParameters();
        /* @var $model Public_PostsModel */
        $model = $this->context->getModel('Posts', 'Public');
        if ($params['tags'] === null || $params['tags'] === '') {
            $tags = array();
        } else {
            $tags = preg_split('/\s*,\s*/', $params['tags']);
        }
        
        $comments_allowed = isset($params['comments_allowed']);
        $published = isset($params['published']);
        
        $model->updateBlogPost($params['post_id'], $params['subject'], $params['body'], 
            $tags, $comments_allowed, $published);
        $post = $model->getPost($params['post_id']);
        $post_coordinates = $model->getPostCoordinates($post);
        $this->setAttribute('post', $post);
        $this->setAttribute('post_coordintates', $post_coordinates);
        
        // clear some cache entries
        $model->clearPostCache($post, array('clear_lists'=>1));
        
        return 'Success';
    }
    
    public function isSecure()
    {
        return true;
    }
    
    public function getCredentials()
    {
        return array('posts.edit');
    }
    
	/**
	 * Returns the default view if the action does not serve the request
	 * method used.
	 *
	 * @return     mixed <ul>
	 *                     <li>A string containing the view name associated
	 *                     with this action; or</li>
	 *                     <li>An array with two indices: the parent module
	 *                     of the view to be executed and the view to be
	 *                     executed.</li>
	 *                   </ul>
	 */
	public function getDefaultViewName()
	{
		return 'Success';
	}
}

?>
